package com.dejian.bloodPressure;

import android.app.Application;
import android.content.Intent;

import com.baidu.mapapi.SDKInitializer;
import com.igexin.sdk.PushManager;
import com.sam.ble.base.application.BLEBRInterfApplication;
import com.sam.ble.unimplements.BLEBRHelper;
import com.sam.map.InitializerMapApplication;
import com.sam.platmform.unimplements.PlatformBRHelper;
import com.sam.gui.activity.MakeAppointmentWebActivity;
//import com.umeng.analytics.MobclickAgent;

import chu.dejian.conf.BLEPreference;
import chu.dejian.db.DBHelper;
import chu.dejian.restApi.RestClient;
import chu.dejian.ui.activity.BLEMainActivity;
import im.fir.sdk.FIR;

public class ThisApplication extends BLEBRInterfApplication {
    public static final boolean DEBUG = false;

    private static ThisApplication instance;
    public synchronized static ThisApplication getInstance() {
        if (instance == null) {
            instance = new ThisApplication();
        }
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
//        MobclickAgent.setScenarioType(getApplicationContext(), MobclickAgent.EScenarioType.E_UM_NORMAL);
        SDKInitializer.initialize(getApplicationContext());
        FIR.init(this);
        BLEPreference.getInstance().init(getApplicationContext());
        PushManager.getInstance().initialize(getApplicationContext());

        BLEBRHelper.getInstance().registerReloginBR(getApplicationContext());
        BLEBRHelper.getInstance().setReloginCallback(new BLEBRHelper.ReloginCallback() {
            @Override
            public void relogin() {
                Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                startActivity(intent);
            }
        });
        BLEBRHelper.getInstance().registerSeekAdviceBR(getApplicationContext());
        BLEBRHelper.getInstance().setSeekAdviceCallback(new BLEBRHelper.SeekAdviceCallback() {
            @Override
            public void seekMedicalAdvice() {
                Intent intent = new Intent(getApplicationContext(), MakeAppointmentWebActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                startActivity(intent);
            }
        });

        PlatformBRHelper.getInstance().registerEnterBLEBR(getApplicationContext());
        PlatformBRHelper.getInstance().setEnterBLECallback(new PlatformBRHelper.EnterBLECallback() {
            @Override
            public void enterBLEModule() {
                Intent intent = new Intent(getApplicationContext(), BLEMainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                startActivity(intent);
            }
        });

        PlatformBRHelper.getInstance().registerLoginSecceededBR(getApplicationContext());
        PlatformBRHelper.getInstance().setLoginSecceededCallback(new PlatformBRHelper.LoginSecceededCallback() {
            @Override
            public void onLoginSucceeded(String account, String passwd, String token, String id) {
                final String preAccount = BLEPreference.getInstance().getAccount(null);
                if(preAccount != null && !preAccount.equals(account)) BLEPreference.getInstance().setMeasurementPage(1);
                BLEPreference.getInstance().setPassword(passwd);
                BLEPreference.getInstance().setToken(token);
                BLEPreference.getInstance().setAccount(account);
                DBHelper.init(getApplicationContext());
                PushManager.getInstance().bindAlias(getApplicationContext(), id);
                RestClient.getUnAuthedClient().setToken(token);
            }
        });


        PlatformBRHelper.getInstance().registerGotoSettingsAction(getApplicationContext());
        PlatformBRHelper.getInstance().setGotoSettingsCallback(new PlatformBRHelper.GotoSettingsCallback() {
            @Override
            public void gotoSettingsPage() {

            }
        });
    }
}
